<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="2"
        :formType="type"
        labelWidth="100"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <el-form ref="addTreeForm" :model="treeForm" label-width="100px" style="font-size: 10px">
      <el-form-item label="来源渠道">
        <el-cascader
          :options="options"
          :props="defaultProps"
          v-model="treeForm.clueSourceId"
          clearable>
        </el-cascader>
      </el-form-item>
    </el-form>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton :click="saveDialog" type="primary">保 存</AjaxButton>
    </div>
  </div>
</template>

<script>
import {
  getCategories,
  getPartitionById,
  getUncounselorList,
  addCluesPrameter,
  editCluesPrameter,
} from '@/api/potenCus/basedata/cluesPrameter';
import { queryClueSourceList } from '@/api/potenCus/customer/potenCusManage';
import { dictionary } from '@/utils/dictMixin';

export default {
  name: 'setAllocationsModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      treeForm: {
        clueSourceId: ''
      },
      defaultProps: {
        multiple: true,
        children: 'children',
        value: 'id',
        label: 'clueName'
      },
      options: []
    };
  },
  created() {
    this.init();
    this.getClueTree();
    this.getNameList();
  },
  methods: {
    init() {
      console.log(this.rowData,"this.rowData")
      if (this.type === 'edit' || this.type === 'delete') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.consultantId;
        // this.isAllot = this.rowData.isAllot ==='是'?1: 0;
        console.log('this.isAllot',this.isAllot)
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
        // console.log(this.rowData.workTime.split('、'),"this.rowData")
        let dataList = this.rowData.workTime.split('、')
        let valueArr=[]
        let itemListArr=[]
        dataList.map( (x,value) =>{
          // console.log('text',x,value);
          switch (x) {
            case '周一':
              valueArr.push(1)
              break;
            case '周二':
              valueArr.push(2)
              break;
            case '周三':
              valueArr.push(3)
              break;
            case '周四':
              valueArr.push(4)
              break;
            case '周五':
              valueArr.push(5)
              break;
            case '周六':
              valueArr.push(6)
              break;
            case '周日':
              valueArr.push(7)
              break;
            default:
              break;
          }
          // valueArr.push(value)
          // itemListArr.push({value: value, text: x})
          // this.formList[6].initialValue=valueArr
        })
        this.formList.find(item => item.fieldName == 'date').initialValue = valueArr
        this.treeForm.clueSourceId = this.rowData['sourceArr']
        // console.log(this.formList[6],"this.formList[6]")
      }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '选择顾问',
          fieldName: 'consultantId',
          placeholder: '请选择',
          disabled: this.type === 'edit',
          initialValue: '',
          itemList: [],
          rules: [{ required: true, message: '请选择顾问', trigger: 'blur' }],
          change: val => {
            this.changePhoneSelect(val);
          }
        },
        {
          type: 'INPUT',
          label: '手机号码',
          fieldName: 'phone',
          disabled: true,
          placeholder: '手机号码',
          initialValue: '',
        },
        {
          type: 'SELECT',
          label: '是否可分配',
          fieldName: 'isAllot',
          placeholder: '是否可分配',
          itemList:[
            {value: 1, text: '是'},
            {value: 0, text: '否'},
          ],
          rules: [{ required: true, message: '请选择是否可分配', trigger: 'blur' }],
          initialValue: '',
        },
        {
          type: 'INPUT_NUMBER',
          label: '分配系数',
          fieldName: 'consultantRatio',
          placeholder: '分配系数',
          initialValue: 1,
          rules: [{ required: true, message: '请输入分配系数', trigger: 'blur' }]
        },
        {
          type: 'MULTIPLE_CHECKBOX',
          label: '服务时间',
          fieldName: 'date',
          placeholder: '请选择',
          selfCols: 2,
          initialValue: [1,2,3,4,5],
          itemList: [
            {value: 1, text: '周一'},
            {value: 2, text: '周二'},
            {value: 3, text: '周三'},
            {value: 4, text: '周四'},
            {value: 5, text: '周五'},
            {value: 6, text: '周六'},
            {value: 7, text: '周日'}
          ],
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async getNameList() {
      const res = await getUncounselorList({consultantId: this.recordId});
      console.log('顾问', res)
      if (res.resultCode === 200) {
        this.formList[0].itemList = res.data.map(x => ({ value: x.empId, text: x.name }));
      }
    },
    async changePhoneSelect(empId) {
      const res = await getUncounselorList(empId);
      if (res.resultCode === 200) {
        var self = res.data
        self.map( (item,index) => {
          console.log('item:',item);
          if(item.empId == empId){
            this.formList.phone = Number(item.phone)
            console.log('phone', this.formList.phone)
            this.formList.forEach(x=>{
              if(x.fieldName == 'phone'){
                x.initialValue = this.formList.phone
              }
            })
          }
        })
      }
    },
    // 线索来源
    async getClueTree() {
      const res = await queryClueSourceList();
      console.log('线索来源', res)
      this.options = res.data
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      let selectList = this.treeForm.clueSourceId ? this.treeForm.clueSourceId.map(item => item[item.length-1]) : []
      console.log('保存', this.saveData.isAllot)
      console.log('saveDialog',this.saveData,selectList)
      if (this.saveData !== null) {
        let res = {};

        if (this.type === 'add') {
          res = await addCluesPrameter({...this.saveData, source: selectList});
        }
        if (this.type === 'edit') {
          if(this.saveData.isAllot =="是" || this.saveData.isAllot ==1){
            this.saveData.isAllot = 1
          } else if(this.saveData.isAllot =="否" || this.saveData.isAllot ==0){
            this.saveData.isAllot = 0
          }
          // this.saveData.isAllot = this.saveData.isAllot ==='是'?1: 0;
          res = await editCluesPrameter({ ...this.saveData, consultantId: this.recordId,source: selectList});
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
          this.$message.success("保存成功!");
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less" scoped>
</style>
